﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace FiberVisible
{
    /// <summary>
    /// 纤维信息
    /// </summary>
    public class FiberInfo
    {
        /// <summary>
        /// 截面材料类型枚举
        /// </summary>
        public enum MatType
        {
            /// <summary>
            /// 混凝土
            /// </summary>
            Concrete = 1,

            /// <summary>
            /// 纵筋
            /// </summary>
            Reinforce = 2,

            /// <summary>
            /// 钢
            /// </summary>
            Steel = 3,
        }

        #region Variables

        /// <summary>
        /// y轴坐标
        /// </summary>
        public float y { set; get; }

        /// <summary>
        /// z轴坐标
        /// </summary>
        public float z { set; get; }

        /// <summary>
        /// 纤维面积
        /// </summary>
        public float A { set; get; }

        /// <summary>
        /// 绘图半径
        /// </summary>
        public float r { set; get; }

        /// <summary>
        /// 记录纤维单元类型
        /// </summary>
        public MatType Type { set; get; }

        /// <summary>
        /// 材料编号
        /// </summary>
        public int MatID { set; get; }

        /// <summary>
        /// 纤维颜色
        /// </summary>
        public Color Color { set; get; }

        #endregion

        /// <summary>
        /// 绘制纤维 主纤维空心绘制 其他纤维实心绘制
        /// </summary>
        /// <param name="g"></param>
        /// <param name="index"></param>
        /// <param name="scale"></param>
        /// <param name="offsety"></param>
        /// <param name="offsetz"></param>
        /// <param name="offsetPicX"></param>
        /// <param name="offsetPicY"></param>
        public void ToGraphis(Graphics g, int index, float offsety, float offsetz, Tuple<float, float, float> scaleTuple)
        {
            //获得纤维绘图坐标与半径
            var posTuple = this.GetFiberPosRaduis(offsety, offsetz, scaleTuple);
            //边长
            float edge_ = posTuple.Item3 * 2;
            //主纤维空心绘制 其他纤维实心绘制
            if (index == 0)
            {
                Pen pen = new Pen(this.Color, 2F);
                g.DrawEllipse(pen, posTuple.Item1, posTuple.Item2, edge_, edge_);
            }
            else
            {
                Brush brush = new SolidBrush(this.Color);
                g.FillEllipse(brush, posTuple.Item1, posTuple.Item2, edge_, edge_);
            }
        }

        /// <summary>
        /// 绘制纤维：高亮显示
        /// </summary>
        /// <param name="g"></param>
        /// <param name="offsety"></param>
        /// <param name="offsetz"></param>
        /// <param name="scaleTuple"></param>
        public void ToGraphis(Graphics g, float offsety, float offsetz, Tuple<float, float, float> scaleTuple)
        {
            //获得纤维绘图坐标与半径
            var posTuple = this.GetFiberPosRaduis(offsety, offsetz, scaleTuple);
            //边长
            float edge_ = posTuple.Item3 * 2;
            Brush brush = new SolidBrush(Color.Red);
            g.FillEllipse(brush, posTuple.Item1, posTuple.Item2, edge_, edge_);
        }

        /// <summary>
        /// 鼠标点击位置是否落在纤维范围内
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="offsety"></param>
        /// <param name="offsetz"></param>
        /// <param name="scaleTuple"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool isSelectFiber(float x, float y, float offsety, float offsetz, Tuple<float, float, float> scaleTuple, ref float error)
        {
            //获得纤维绘图坐标与半径
            var posTuple = this.GetFiberPosRaduis(offsety, offsetz, scaleTuple);
            //圆心实际坐标
            var centrolX = posTuple.Item1 + this.r * scaleTuple.Item1;
            var centrolY = posTuple.Item2 + this.r * scaleTuple.Item1;
            //计算纤维中心到鼠标点击点的距离
            float disp = (float)(Math.Pow(x - centrolX, 2) + Math.Pow(y - centrolY, 2));
            //是否落在范围内
            return (Math.Abs(x - centrolX) <= posTuple.Item3) && (Math.Abs(y - centrolY) <= posTuple.Item3);
        }

        /// <summary>
        /// 获得纤维绘图坐标与半径
        /// </summary>
        /// <param name="offsety"></param>
        /// <param name="offsetz"></param>
        /// <param name="scaleTuple"></param>
        /// <returns></returns>
        private Tuple<float, float, float> GetFiberPosRaduis(float offsety, float offsetz, Tuple<float, float, float> scaleTuple)
        {
            //截面缩小比例
            float scale = scaleTuple.Item1;
            //坐标补偿
            float offsetPicX = scaleTuple.Item2;
            float offsetPicY = scaleTuple.Item3;
            //半径
            float radis = this.r  * scale;
            //左上角坐标
            float x = (this.y - offsety - this.r) * scale + offsetPicX;
            float y = (this.z - offsetz + this.r) * -scale + offsetPicY;
            //返回坐标
            return Tuple.Create<float, float, float>(x, y, radis);
        }

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mat_id"> 材料编号 </param>
        /// <param name="y_"> y轴坐标 </param>
        /// <param name="z_"> z轴坐标 </param>
        /// <param name="a_"> 纤维面积 </param>
        public FiberInfo(int mat_id, float y, float z, float a)
        {
            this.MatID = mat_id;
            this.y = y;
            this.z = z;
            this.A = a;
            this.r = (float)Math.Sqrt(this.A / Math.PI);
        }

        /// <summary>
        /// 空白的构造函数
        /// </summary>
        public FiberInfo()
        {

        }

        #endregion

    }
}
